(function($) {
	function cartMove() {
		if ($("#cartNav").hasClass('closed')) {
			$('.navbar-cart').animate({
				right : '-20%'
			}, 300);
			$("#cartNav").removeClass('closed');
			$("#cartNav").animate({
				right : '0%'
			}, 300);

		} else {
			$("#cartNav").addClass('closed');
			$('.navbar-cart').animate({
				right : '0%'
			}, 300);
			$("#cartNav").animate({
				right : '20%'
			}, 300);
		}
	}

	function cartMove2() {
		$('.navbar-cart').animate({
			right : '0%'
		}, 300);
		$("#cartNav").animate({
			right : '20%'
		}, 300);

	}

	$(document).ready(function() {
		$("#cartNav").click(function() {
			cartMove();
		});
	});

	dish = function dish(mid, did, action) {
		$.ajax({
			type : "POST",
			data : {
				"mid" : mid,
				"did" : did,
				"action" : action
			},
			url : "../cart/dish",
			success : function(data) {
				cartMove2();
				if (data.status) {
					if (action == "add") {
						if ($('#cart-table').find('#cart' + did).length != 0) {
							$('#cart' + did).find('.counts')[0].value++;
							total();

						} else {
							cartModel(mid, did);
							total();
						}
					} else {
						$('#cart' + did).find('.counts')[0].value--;
						total();
						if ($('#cart' + did).find('.counts')[0].value == 0) {
							$('#cart' + did).remove();
						}
					}
				}
			}
		});
	}

	cartModel = function cartModel(mid, did) {
		var imgSrc = $('#' + did).find("img")[0].currentSrc;
		var dname = $('#' + did).find('.caption').find("h3")[0].innerHTML;
		var dprice = $('#' + did).find('.price').find("span")[0].innerHTML;

		var dishTr = $("<tr></tr>");

		dishTr.addClass('dish');
		dishTr.attr('id', "cart" + did);

		var imgTd = $("<td></td>");
		imgTd.addClass('dish-img');
		var dishImg = $("<div><img></div>").appendTo(imgTd);
		dishImg.find("img")[0].src = imgSrc;

		var infoTd = $("<td><p></p><p></P></td>");
		infoTd.addClass('dish-info');
		infoTd.find("p")[0].innerHTML = dname;
		infoTd.find("p")[1].innerHTML = "$";
		var price = $("<span></span>").appendTo(infoTd.find("p")[1]);
		price.addClass('dish-price');
		price.html(dprice);

		var countTd = $("<td></td>");
		countTd.addClass('dish-count');
		var minesSpan = $("<span></span>");
		minesSpan.addClass('mines');
		minesSpan.html("-");
		minesSpan.bind("click", function() {
			dish(mid, did, 'sub');
		});
		var countInput = $("<input>");
		countInput.addClass('counts')
		countInput.val(1);
		countInput.bind("keyup",
				function() {
					if (!isNaN($('#cart' + did).find('.counts')[0].value)) {
						countChange(mid, did,
								$('#cart' + did).find('.counts')[0].value);
					}
				});

		var plusSpan = $("<span></span>");
		plusSpan.addClass('plus');
		plusSpan.html("+");
		plusSpan.bind("click", function() {
			dish(mid, did, 'add');
		});
		countTd.append(minesSpan);
		countTd.append(countInput);
		countTd.append(plusSpan);

		dishTr.append(imgTd);
		dishTr.append(infoTd);
		dishTr.append(countTd);
		dishTr.appendTo($('#cart-table'));
	}

	countChange = function countChange(mid, did, count) {
<<<<<<< HEAD
		$.ajax({
			type : "POST",
			data : {
				"mid" : mid,
				"did" : did,
				"count" : count
			},
			url : "../cart/update",
			success : function(data) {
				if (data.status) {
					if ($('#cart' + did).find('.counts')[0].value == 0) {
						$('#cart' + did).remove();
=======
		if (!isNaN(count)) {
			$.ajax({
				type : "POST",
				data : {
					"mid" : mid,
					"did" : did,
					"count" : count
				},
				url : "../cart/update",
				success : function(data) {
					if (data.status) {
						if ($('#cart' + did).find('.counts')[0].value == 0) {
							$('#cart' + did).remove();
						}
						total();
>>>>>>> branch 'master' of https://git.oschina.net/tmaczhou/IOSC2.git
					}
				}
			});
		}
		
	}

	total = function total() {
		var prices = $('#cart-table').find('.dish').find('.dish-price');
		var counts = $('#cart-table').find('.dish').find('.counts');
		var total = 0;

		for (var i = prices.length - 1; i >= 0; i--) {
			total += (Number(prices[i].innerHTML) * Number(counts[i].value));
		}
		$('.checkout').find('.total')[0].innerHTML = "Total: " + total + "";
	}

}(jQuery));
